﻿Public Class frmListadoCliente

 
    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub frmListadoCliente_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: esta línea de código carga datos en la tabla 'CaserDataSet.personas' Puede moverla o quitarla según sea necesario.
        Me.PersonasTableAdapter.Fill(Me.CaserDataSet.personas)
        cmbTipo.SelectedIndex = 0
    End Sub


   
    Private Sub Filtrar()
        Dim tipoSi = tomarValores(lstSi.Items)
        Dim tipoNo = tomarValores(lstNo.Items)
        Dim sError As String = ""


        sError = CargarVistaPolizaFlitro(Me.CaserDataSet.VistaPolizaFiltro)

        If sError <> "" Then
            MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            Dim ids As String
            If (tipoSi = "" And tipoNo = "") Then
                PersonasBindingSource.Filter = ""
            Else

                ids = ""
                If (tipoSi = "") Then
                    tipoSi = "''"
                End If
                If (tipoNo = "") Then
                    tipoNo = "''"
                End If

                Dim dtTabla = CaserDataSet.VistaPolizaFiltro.Select("tipo in (" & tipoSi & ")")
                For Each dataR As caserDataSet.VistaPolizaFiltroRow In dtTabla
                    If (CaserDataSet.VistaPolizaFiltro.Select("tipo in (" & tipoSi & ") and idPersona = " & dataR.idpersona).Count >= lstSi.Items.Count) Then
                        ids += "'" & dataR.idpersona & "',"
                    End If

                Next

                Dim idn = tomarValores(CaserDataSet.VistaPolizaFiltro.Select("tipo in (" & tipoNo & ")"))
                If ids <> "" And idn <> "" Then

                    ids = ids.Remove(ids.Length - 1, 1)
                    PersonasBindingSource.Filter = "id in (" & ids & ") and id not in (" & idn & ")"
                ElseIf ids <> "" And idn = "" Then
                    ids = ids.Remove(ids.Length - 1, 1)
                    PersonasBindingSource.Filter = "id in (" & ids & ")"
                ElseIf idn <> "" And ids = "" Then
                    PersonasBindingSource.Filter = "id not in (" & idn & ")"
                Else
                    If (tipoSi <> "''") Then
                        PersonasBindingSource.Filter = "id = 0"
                    Else
                        PersonasBindingSource.Filter = ""
                    End If

                End If


            End If

        End If

    End Sub


    Private Sub btnAddSi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSi.Click
        If (cmbTipo.SelectedItem IsNot Nothing) Then
            lstSi.Items.Add(cmbTipo.SelectedItem)
            cmbTipo.Items.Remove(cmbTipo.SelectedItem)
            If (cmbTipo.Items.Count > 0) Then
                cmbTipo.SelectedIndex = 0
            Else
                cmbTipo.ResetText()
            End If
            If (cmbTipo.Items.Count > 0) Then
                lstSi.SelectedIndex = lstSi.Items.Count - 1
            End If

        End If
       
        Filtrar()

    End Sub

    Private Sub btnDelSi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelSi.Click
        If (lstSi.SelectedItem IsNot Nothing) Then
            cmbTipo.Items.Add(lstSi.SelectedItem)
            lstSi.Items.Remove(lstSi.SelectedItem)
            If (cmbTipo.Items.Count > 0) Then
                cmbTipo.SelectedIndex = 0
            Else
                cmbTipo.ResetText()
            End If
            If (cmbTipo.Items.Count > 0) Then
                lstSi.SelectedIndex = lstSi.Items.Count - 1
            End If
        End If



        Filtrar()
    End Sub

    Private Sub btnAddNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNo.Click
        If (cmbTipo.SelectedItem IsNot Nothing) Then
            lstNo.Items.Add(cmbTipo.SelectedItem)
            cmbTipo.Items.Remove(cmbTipo.SelectedItem)
            If (cmbTipo.Items.Count > 0) Then
                cmbTipo.SelectedIndex = 0
            Else
                cmbTipo.ResetText()

            End If
            If (cmbTipo.Items.Count > 0) Then
                lstNo.SelectedIndex = lstNo.Items.Count - 1
            End If
        End If
        Filtrar()
    End Sub

    Private Sub btnDelNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelNo.Click
        If (lstNo.SelectedItem IsNot Nothing) Then
            cmbTipo.Items.Add(lstNo.SelectedItem)
            lstNo.Items.Remove(lstNo.SelectedItem)
            If (cmbTipo.Items.Count > 0) Then
                cmbTipo.SelectedIndex = 0
            Else
                cmbTipo.ResetText()
            End If
            If (cmbTipo.Items.Count > 0) Then
                lstNo.SelectedIndex = lstNo.Items.Count - 1
            End If
        End If
        Filtrar()
    End Sub

  
    Private Function tomarValores(ByVal objectCollection As ICollection) As String
        Dim result As String
        If objectCollection.Count <> 0 Then
            result = ""
            For Each item As Object In objectCollection
                If (TypeOf item Is DataRow) Then
                    
                    result += item("idpersona") & ","
                Else
                    result += "'" & item & "',"
                End If


            Next
            result = result.Remove(result.Length - 1, 1)
        Else
            result = ""
        End If
        Return result
    End Function

   
    

 

    
    Private Sub dgPersonas_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgPersonas.SelectionChanged

        If dgPersonas.CurrentRow IsNot Nothing Then
            VistaPolizasPorClienteBindingSource.Filter = ""

            Dim rowPersona = CType(dgPersonas.CurrentRow.DataBoundItem.row, caserDataSet.personasRow)

            Dim sError As String = ""


            sError = CargarVistaPolizaPorCliente(CaserDataSet.VistaPolizasPorCliente, rowPersona.Id)

            If sError <> "" Then
                MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Else
            VistaPolizasPorClienteBindingSource.Filter = "id = 0"



        End If
    End Sub



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If dgPersonas.CurrentRow IsNot Nothing Then
            If CType(Me.MdiParent, Principal).existeForm("frmGestionClientes") Then
                Dim Formulario As Form

                For Each Formulario In Me.MdiParent.MdiChildren
                    If Formulario.Name = "frmGestionClientes" Then
                        Formulario.Close()
                        Exit For
                    End If
                Next Formulario
            End If
            Dim frmCliente As frmGestionClientes
            frmCliente = New frmGestionClientes
            frmCliente.MdiParent = Me.MdiParent
            frmCliente.Tag = "0;" & CType(dgPersonas.CurrentRow.DataBoundItem.row, caserDataSet.personasRow).Id
            frmCliente.Show()

        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        PrintDGV.Print_DataGridView(dgPersonas, "Clientes con filtro", Nothing)
    End Sub
End Class